package ntd.util.str;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import ntd.util.ToolCache;
import ntd.util.cache.FactoryCache;
import ntd.util.cache.ComCache;
import ntd.util.str.bo.CutMsg;

import java.util.ArrayList;
import java.util.List;

@Data
@Accessors(chain = true)
public class ToolCutMsg {

    private static final ComCache CACHE = FactoryCache.cache("tool-cut-msg-cache");

    private int maxLength = 2000;
    private List<CutMsg> cutMsgList = new ArrayList<>();
    private String currentMsg = "";

    public static ToolCutMsg me(List<CutMsg> cutMsgList) {
        return new ToolCutMsg().setCutMsgList(cutMsgList);
    }

    public static ToolCutMsg me(CutMsg cutMsg) {
        return me(ListUtil.toList(cutMsg));
    }

    public List<String> parseDeep(String appendStr) {
        List<String> cutMsgs = new ArrayList<>();
        CutMsg parse = parse(appendStr);
        if (ObjectUtil.isNull(parse)) {
            return cutMsgs;
        } else {
            cutMsgs.add(parse.getResult());
        }
        for (int i = 0; i < 20; i++) {
            CutMsg cutMsg = parse("");
            if (ObjectUtil.isNull(cutMsg)) {
                return cutMsgs;
            }
            cutMsgs.add(cutMsg.getResult());
        }
        return cutMsgs;
    }

    public CutMsg parse(String appendStr) {
        currentMsg += appendStr;
        if (StrUtil.isEmpty(currentMsg)) {
            return null;
        }
        for (CutMsg cutMsg : cutMsgList) {
            if (ObjectUtil.isNull(cutMsg)) {
                continue;
            }
            int startIndex = currentMsg.indexOf(cutMsg.getStartTag());
            if (startIndex < 0) {
                continue;
            }
            int endIndex = cutMsg.getEndIndex(startIndex, currentMsg);
            if (endIndex < 0) {
                continue;
            }
            String result = currentMsg.substring(startIndex, endIndex);
            cutMsg.setResult(result);
            currentMsg = currentMsg.substring(endIndex);
            return cutMsg;
        }
        // 最长2000，超出直接舍弃
        currentMsg = StrUtil.subSufByLength(currentMsg, 5000000);
        return null;
    }

    public static void main(String[] args) {
        String str = "##0351ST=32;CN=2051;QN=20230227154142986;PW=123456;MN=410;Flag=1;CP=&&DataTime=20230227154000;B01-Min=0.00,B01-Avg=0.00,B01-Max=0.00,B01-Cou=0.00;011-Min=0.000,011-Avg=0.000,011-Max=0.000,011-Cou=0.000;101-Min=0.000,101-Avg=0.000,101-Max=0.000,101-Cou=0.000;001-Min=0.000,001-Avg=0.000,001-Max=0.000;060-Min=0.000,060-Avg=0.000,060-Max=0.000,060-Cou=0.000&&E401\r\n" +
                "##0246ST=32;CN=2011;QN=20230227154145107;PW=123456;MN=410;Flag=0;CP=&&DataTime=20230227154145;B01-Rtd=0.00,B01-Flag=N;BO2-Cou=_,BO2-Flag=N;011-Rtd=0.000000,011-Flag=N;101-Rtd=0.000000,101-Flag=N;001-Rtd=0.000000,001-Flag=N;060-Rtd=0.000000,060-Flag=N&&BF41\r\n";

        ToolCutMsg me = ToolCutMsg.me(new CutMsg().setStartTag("##").setEndTag("\r\n"));
//        System.out.println(me.parse(HexUtil.decodeHexStr("232330343339514E3D32303232303932303134343131363434343B53543D32313B434E3D323036313B50573D323332323B4D4E3D343130373237313030313B466C61673D393B43503D26264461746154696D653D32303232303931343034303030303B7730313031392D4176673D31302E3039352C7730313031392D466C61673D4E3B7730313032302D4176673D31332E30312C7730313032302D466C61673D", CharsetUtil.CHARSET_UTF_8)));
//        System.out.println(me.parse(HexUtil.decodeHexStr("4E3B7732313030312D4176673D312E3030302C7732313030312D466C61673D4E3B7732313031312D4176673D302E3330392C7732313031312D466C61673D4E3B7730313031382D4176673D33332E3638352C7730313031382D466C61673D4E3B7730313031342D4176673D333130382E31352C7730313031342D466C61673D4E3B7730313030332D4176673D3534342E36362C7730313030332D466C61673D4E", CharsetUtil.CHARSET_UTF_8)));
        System.out.println(JSONUtil.toJsonStr(me.parseDeep(str)));
//        System.out.println(me.parse(HexUtil.decodeHexStr("232330313234514E3D32303232303932303131323233393732323B53543D32313B434E3D333032303B50573D323332323B4D4E3D343130373237313030313B466C61673D393B43503D26264461746154696D653D32303137303131373136323031333B506F6C49643D7730313031392C6931333030312D496E666F3D302E30302626463530310D0A")));
//        System.out.println(me.parse("5135866615"));
//        System.out.println(me.parse("5135556315"));
//        System.out.println(me.parse("5135863615"));
//        System.out.println(me.parse("5135846315"));
//        System.out.println(me.parse("5135866615"));
//        System.out.println(me.parse("555366685557666"));
//        System.out.println(me.parse("##0190QN=20220623135203982;ST=21;CN=2011;PW=123456;MN=33010990392983;Flag=5;CP=&&DataTime=20220623135200;w01018-Rtd=231.2,w01018-Flag=N;w01001-Rtd=7.3,w01001-Flag=N;w01010-Rtd=22.5,w01010-Flag=N&&2281\r\n"));


//        String hexStr = "23233032343653543d33323b434e3d323031313b514e3d32303233303232373135343030393838353b50573d3132333435363b4d4e3d3431303b466c61673d303b43503d26264461746154696d653d32303233303232373135343030393b4230312d5274643d302e30302c4230312d466c61673d4e3b424f322d436f753d5f2c424f322d466c61673d4e3b3031312d5274643d302e3030303030302c3031312d466c61673d4e3b3130312d5274643d302e3030303030302c3130312d466c61673d4e3b3030312d5274643d302e3030303030302c3030312d466c61673d4e3b3036302d5274643d302e3030303030302c3036302d466c61673d4e2626393734310d0a\n" +
//                "23233033353153543d33323b434e3d323035313b514e3d32303233303232373135343031313935393b50573d3132333435363b4d4e3d3431303b466c61673d313b43503d26264461746154696d653d32303233303232373135343030303b4230312d4d696e3d302e30302c4230312d4176673d302e30302c4230312d4d61783d302e30302c4230312d436f753d302e30303b3031312d4d696e3d302e3030302c3031312d4176673d302e3030302c3031312d4d61783d302e3030302c3031312d436f753d302e3030303b3130312d4d696e3d302e3030302c3130312d4176673d302e3030302c3130312d4d61783d302e3030302c3130312d436f753d302e3030303b3030312d4d696e3d302e3030302c3030312d4176673d302e3030302c3030312d4d61783d302e3030303b3036302d4d696e3d302e3030302c3036302d4176673d302e3030302c3036302d4d61783d302e3030302c3036302d436f753d302e3030302626464130310d0a";
//
//        String s = HexUtil.decodeHexStr(hexStr);
//
//        StaticLog.info(s);
    }

}
